﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Configuration;

namespace Infrastructure
{
    public class OfficeHelper
    {
        public static void ExportCSV(DataTable dataTable, string excelFileName)
        {
            StringBuilder sbData = new StringBuilder();

            foreach (DataColumn column in dataTable.Columns)
            {
                string cn = string.Empty;
                switch (column.ColumnName)
                {
                    case "Design_No": cn = "图纸号"; break;
                    case "Design_Name": cn = "图纸名称"; break;
                    case "Light_No": cn = "棚灯号"; break;
                    default:
                        break;
                }
                if (column.ColumnName.ToUpper().StartsWith("BUTTON")) continue;
                sbData.Append(cn + ",");
            }
            sbData.AppendLine();

            foreach (DataRow row in dataTable.Rows)
            {
                foreach (DataColumn column in dataTable.Columns)
                {
                    sbData.Append(row[column].ToString() + ",");
                }
                sbData.AppendLine();
            }
            sbData.AppendLine();

            using (FileStream fs = new FileStream(excelFileName, FileMode.Create, FileAccess.Write))
            {
                byte[] buffer = System.Text.Encoding.GetEncoding(ConfigurationManager.AppSettings["CSVEncoding"]).GetBytes(sbData.ToString());
                fs.Write(buffer, 0, buffer.Length);
            }
        }
    }
}
